package com.jhws.business.property.service;

import com.jhws.business.property.bo.PropertyFeeFormula;
import com.jhws.core.db.BaseDao;
import com.jhws.util.GetId;
import com.jhws.util.LogHelper;
import com.jhws.util.StringUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PropertyFeeFormulaService {
    @Resource
    private BaseDao BaseDao;

    /**
     * 保存物业缴费公式
     *
     * @param propertyFeeFormula 公式信息
     */
    public void savePropertyFeeFormula(PropertyFeeFormula propertyFeeFormula) {
        if (StringUtils.isEmpty(propertyFeeFormula.getId())) {
            propertyFeeFormula.setCreationTime(new Date());
            propertyFeeFormula.setId(GetId.randomID());
        }
        BaseDao.saveOrUpdate(propertyFeeFormula);
    }

    /**
     * 分页查询公式信息
     *
     * @param param Map
     * @return 公式信息列表
     */
    @SuppressWarnings("unchecked")
    public Map queryPropertyFeeFormulaPage(Map param) {
        String hql = "from PropertyFeeFormula where 1=1 ";
        String commCode = MapUtils.getString(param, "commCode");
        if (StringUtils.isNotEmpty(commCode)) {
            hql += "and comm_code='" + commCode + "' ";
        }
        String name = MapUtils.getString(param, "name");
        if (StringUtils.isNotEmpty(name)) {
            hql += "and name like '%" + name + "%' ";
        }
        return BaseDao.queryPage(hql);
    }

    /**
     * 条件公式信息
     *
     * @param where 条件
     * @return
     */
    public List<PropertyFeeFormula> queryPropertyFeeFormulaWhere(String where) {
        return BaseDao.findList("from PropertyFeeFormula " + where);
    }

    /**
     * 保存更新使用公式
     * @param id
     * @param commCode
     * @param isEnable
     */
    public void saveEnableFormula(String id, String commCode, boolean isEnable) {
        String hql;
        if (isEnable) {
            hql = "UPDATE PropertyFeeFormula SET isEnable='0' WHERE isEnable='1' and commCode='" + commCode + "'";
            BaseDao.executeHql(hql);
            hql = "UPDATE PropertyFeeFormula SET isEnable='1' WHERE id='" + id + "'";
        } else {
            hql = "UPDATE PropertyFeeFormula SET isEnable='0' WHERE id='" + id + "'";
        }
        BaseDao.executeHql(hql);
    }

    /**
     * 删除公式信息
     *
     * @param propertyFeeFormula 公式信息
     */
    public void deletePropertyFeeFormula(PropertyFeeFormula propertyFeeFormula) {
        BaseDao.delete(propertyFeeFormula);
    }

    /**
     * 删除公式信息
     *
     * @param id 公式信息ID
     */
    public void deletePropertyFeeFormula(String id) {
        PropertyFeeFormula propertyFeeFormula = new PropertyFeeFormula();
        propertyFeeFormula.setId(id);
        deletePropertyFeeFormula(propertyFeeFormula);
    }
}
